﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace StableDiffusionTools.Utils
{
    public static class Base64Util
    {
        public static string ConvertFileToBase64(string file)
        {
            return Convert.ToBase64String(File.ReadAllBytes(file));
        }

        public static async Task<string> ConvertFileToBase64Async(string file)
        {
            using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 125 * 4096, true))
            {
                using (var memStream = new MemoryStream((int)fileStream.Length))
                {
                    await fileStream.CopyToAsync(memStream).ConfigureAwait(false);
                    await fileStream.FlushAsync().ConfigureAwait(false);
                    return Convert.ToBase64String(memStream.GetBuffer());
                }
            }
        }

        //unused
        //public static string ConvertImageToBase64(Bitmap img)
        //{
        //    using (MemoryStream ms = new MemoryStream()) 
        //    {
        //        img.Save(ms, img.RawFormat);
        //        return Convert.ToBase64String(ms.GetBuffer());
        //    }
        //}
    }
}
